﻿namespace S7.Net.Types
{
    using S7.Net;
    using System;

    public static class Double
    {
        private static byte? BinStringToByte(string txt)
        {
            int startIndex = 0;
            int num2 = 0;
            if (txt.Length == 8)
            {
                for (startIndex = 7; startIndex >= 0; startIndex += -1)
                {
                    if (int.Parse(txt.Substring(startIndex, 1)) == 1)
                    {
                        num2 += (int) Math.Pow(2.0, (double) ((txt.Length - 1) - startIndex));
                    }
                }
                return new byte?((byte) num2);
            }
            return null;
        }

        public static double FromByteArray(byte[] bytes)
        {
            byte num = bytes[0];
            byte num2 = bytes[1];
            byte num3 = bytes[2];
            byte num4 = bytes[3];
            if ((((num + num2) + num3) + num4) == 0)
            {
                return 0.0;
            }
            string str = ValToBinString(num) + ValToBinString(num2) + ValToBinString(num3) + ValToBinString(num4);
            int num5 = int.Parse(str.Substring(0, 1));
            int num6 = str.Substring(1, 8).BinStringToInt32();
            string str2 = str.Substring(9, 0x17);
            double num7 = 1.0;
            double num8 = 1.0;
            for (int i = 0; i <= 0x16; i++)
            {
                num8 /= 2.0;
                if (str2.Substring(i, 1) == "1")
                {
                    num7 += num8;
                }
            }
            return ((Math.Pow(-1.0, (double) num5) * Math.Pow(2.0, (double) (num6 - 0x7f))) * num7);
        }

        public static double FromDWord(int value)
        {
            return FromByteArray(DInt.ToByteArray(value));
        }

        public static double FromDWord(uint value)
        {
            return FromByteArray(DWord.ToByteArray(value));
        }

        public static double[] ToArray(byte[] bytes)
        {
            double[] numArray = new double[bytes.Length / 4];
            int num = 0;
            for (int i = 0; i < (bytes.Length / 4); i++)
            {
                numArray[i] = FromByteArray(new byte[] { bytes[num++], bytes[num++], bytes[num++], bytes[num++] });
            }
            return numArray;
        }

        public static byte[] ToByteArray(double value)
        {
            double d = value;
            string str = "";
            byte[] buffer = new byte[4];
            if (d != 0.0)
            {
                if (d < 0.0)
                {
                    d *= -1.0;
                    str = "1";
                }
                else
                {
                    str = "0";
                }
                int num2 = (int) Math.Floor((double) (Math.Log(d) / Math.Log(2.0)));
                d = (d / Math.Pow(2.0, (double) num2)) - 1.0;
                str = str + ValToBinString((byte) (num2 + 0x7f));
                for (int i = 1; i <= 0x17; i++)
                {
                    if ((d - Math.Pow(2.0, (double) -i)) >= 0.0)
                    {
                        d -= Math.Pow(2.0, (double) -i);
                        str = str + "1";
                    }
                    else
                    {
                        str = str + "0";
                    }
                }
                buffer[0] = BinStringToByte(str.Substring(0, 8)).Value;
                buffer[1] = BinStringToByte(str.Substring(8, 8)).Value;
                buffer[2] = BinStringToByte(str.Substring(0x10, 8)).Value;
                buffer[3] = BinStringToByte(str.Substring(0x18, 8)).Value;
                return buffer;
            }
            buffer[0] = 0;
            buffer[1] = 0;
            buffer[2] = 0;
            buffer[3] = 0;
            return buffer;
        }

        public static byte[] ToByteArray(double[] value)
        {
            ByteArray array = new ByteArray();
            foreach (double num in value)
            {
                array.Add(ToByteArray(num));
            }
            return array.array;
        }

        private static string ValToBinString(byte value)
        {
            string str = "";
            for (int i = 7; i >= 0; i += -1)
            {
                if ((value & ((byte) Math.Pow(2.0, (double) i))) > 0)
                {
                    str = str + "1";
                }
                else
                {
                    str = str + "0";
                }
            }
            return str;
        }
    }
}

